package com.yonyou.le.unattended.websocket;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.web.socket.WebSocketSession;

import com.google.gson.Gson;
import com.yonyou.le.unattended.entity.meta.CarInfoData;

public class TestServlet extends HttpServlet {
	public static void main(String[] args) {
		CarInfoData md = new CarInfoData();
		md.setCarno("京A 888888");
		md.setCompany("峨胜集团");
		md.setGrossweight(String.format("%.2f", Math.random() * 100 ));
		md.setId("1");
		md.setMaterial("水泥");
		md.setNetweight(String.format("%.2f", Math.random() * 100));
		md.setOpcode("ZYD001");
		md.setTareweight(String.format("%.2f", Math.random() * 100 ));
		md.setSelfhandstatus("0");
		md.setNotiMessage("豫A00001");
		System.out.println(new Gson().toJson(md));
	}
	/** 
     *  
     */
	private static final long serialVersionUID = 5260036616763563895L;
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss"); 

	/**
	 * Constructor of the object.
	 */
	public TestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		System.out.println("destroy");
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// 测试-周期性让前台更新作业点状态
//		System.out.println("启动测试servelet");
//		System.out.println(this.getServletConfig().getServletContext().getRealPath("/"));
		//final String[] OP = { "ZYD001", "ZYD002", "ZYD003", "ZYD004"};
		final String[] OP = { "01","01","01","01"};
		final String[] msgs = { "磅上有车，请退后", "地感一被挡，有车上磅", "停止读卡", "地感二被挡，有车上磅"};
		final String[] es = {"","车太重了","车太长了"};
		
		Runnable r0 = new Runnable() {

			@Override
			public void run() {
				for(int i=0;i<4;i++){
					String opcode = OP[i];
					Map<String, WebSocketSession> session = MonitorUtils
							.getSession(opcode);
					if (session == null)
						continue;
					
					// wss.sendMessage(arg0);
					Gson gson = new Gson();
					try {
						CarInfoData md = getCar(opcode, i);
						MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARINFO.getName(), opcode,
								gson.toJson(md));
						
						MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_WEIGHT.getName(), opcode,md.getGrossweight());
					
					
						int t=(int)(Math.random()*4);
						String msg = formatter.format(new Date())+":" + msgs[t];
						MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARMSG.getName(), opcode, msg);
						
						
						
						MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARSTATUS.getName(), opcode, i+"");
						
						String e = es[(int) (Math.random() * 3)] ;
						//MsgException msgException=new MsgException(e,e,e);
						MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_EXCEPTIONS.getName(), opcode, e);
					} catch (IOException e1) {
						// TODO 自动生成的 catch 块
						e1.printStackTrace();
					}
				}
				
			}
			
		};
		
		
		
		
		
		Runnable r1 = new Runnable() {
			@Override
			public void run() {
				String opcode = OP[(int) (Math.random() * 6)];
				Map<String, WebSocketSession> session = MonitorUtils
						.getSession(opcode);
				if (session == null)
					return;
				CarInfoData md = new CarInfoData();
				md.setCarno("京A 888888");
				md.setCompany("峨胜集团");
				md.setGrossweight(String.format("%.2f", Math.random() * 100 ));
				md.setId("1");
				md.setMaterial("水泥");
				md.setNetweight(String.format("%.2f", Math.random() * 100));
				md.setOpcode(opcode);
				md.setTareweight(String.format("%.2f", Math.random() * 100 ));
				// wss.sendMessage(arg0);
				Gson gson = new Gson();
				
				try {
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARINFO.getName(), opcode,
							gson.toJson(md));
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_WEIGHT.getName(), opcode,md.getGrossweight());
				
				
					int t=(int)(Math.random()*4);
					String msg = formatter.format(new Date())+":" + msgs[t];
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARMSG.getName(), opcode, msg);
					
					
					String status = (int) (Math.random() * 4) + "";
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARSTATUS.getName(), opcode, status);
					
					String e = es[(int) (Math.random() * 3)] ;
					MsgException msgException=new MsgException(e,e,e);
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_EXCEPTIONS.getName(), opcode, gson.toJson(msgException));
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
		};

		Runnable r2 = new Runnable() {
			public void run() {
				String opcode = OP[(int) (Math.random() * 6)];
				Map<String, WebSocketSession> session = MonitorUtils
						.getSession(opcode);
				if (session == null)
					return;
				int t=(int)(Math.random()*4);
				String msg = formatter.format(new Date()) + msgs[t];
				try {
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARMSG.getName(), opcode, msg);
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
		};

		Runnable r3 = new Runnable() {
			public void run() {
				String opcode = OP[(int) (Math.random() * 6)];
				Map<String, WebSocketSession> session = MonitorUtils
						.getSession(opcode);
				if (session == null)
					return;

				String status = (int) (Math.random() * 6) + "";
				try {
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_CARSTATUS.getName(), opcode, status);
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
		};
		Runnable r4 = new Runnable() {
			public void run() {
				String opcode = OP[(int) (Math.random() * 6)];
				Map<String, WebSocketSession> session = MonitorUtils
						.getSession(opcode);
				if (session == null)
					return;

				String e = es[(int) (Math.random() * 3)] ;
				try {
					MonitorUtils.sendMessage(MsgTypeEnum.WS_TYPE_EXCEPTIONS.getName(), opcode, e);
				} catch (IOException e1) {
					e1.printStackTrace();
				}

			}
		};
//		ScheduledExecutorService newSingleThreadScheduledExecutor = Executors
//				.newSingleThreadScheduledExecutor();
//		newSingleThreadScheduledExecutor.scheduleAtFixedRate(r0, 1000, 400,
//				TimeUnit.MILLISECONDS);
		
		
//		newSingleThreadScheduledExecutor.scheduleAtFixedRate(r1, 1000, 400,
//				TimeUnit.MILLISECONDS);
//		newSingleThreadScheduledExecutor.scheduleAtFixedRate(r2, 1100, 500,
//				TimeUnit.MILLISECONDS);
//		newSingleThreadScheduledExecutor.scheduleAtFixedRate(r3, 1200, 600,
//				TimeUnit.MILLISECONDS);
//		newSingleThreadScheduledExecutor.scheduleAtFixedRate(r4, 1300, 700,
//				TimeUnit.MILLISECONDS);
	}
	private CarInfoData getCar(String opcode,int i){
		CarInfoData md = new CarInfoData();
		
		md.setCompany("峨胜集团");
		md.setOpcode(opcode);
		md.setId("1");
		
		if(i==0){
			return new CarInfoData();
		}else if(i == 1){
			md.setCarno("京A 888881");
			md.setGrossweight("0.00");
			md.setMaterial("水泥");
			md.setTareweight("0.00");
			md.setNetweight("0.00");
		}else if(i == 2){
			md.setCarno("京A 888885");
			md.setGrossweight("40.50");
			md.setMaterial("煤");
			md.setTareweight("2.00");
			md.setNetweight("0.00");
		}else if(i == 3){
			md.setCarno("京A 888888");
			md.setGrossweight("40.50");
			md.setMaterial("煤");
			md.setTareweight("2.00");
			md.setNetweight("38.50");
		}
		return md;
		

		
	}
}
